<!--
 * @Description:
 * @version:
 * @Author: sunqian_sr
 * @Date: 2022-12-15 16:54:04
 * @LastEditors: Andy
 * @LastEditTime: 2023-01-13 10:01:53
-->
<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="35%">
    <el-form :model="params" class="demo-form-inline" ref="params" :rules="rules" label-position="top">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item :label="t('organization.byOrganiz.name')" prop="purchaseGroupName">
            <el-input
              v-model="params.purchaseGroupName"
              :placeholder="$t('system.user.remarktxt')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="t('organization.byOrganiz.code')" prop="purchaseGroupCode">
            <el-input
              v-model="params.purchaseGroupCode"
              :placeholder="$t('system.user.remarktxt')"
              clearable
            ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('organization.byOrganiz.toStart')" prop="state">
            <!-- <x-radio-group :list-data="dictOptions.state" v-model="params.state"></x-radio-group> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading"> {{ $t('common.confirm') }} </el-button>

      <!-- <el-button v-if="'id' in params" type="danger" @click="handleDeleteOrg"> 删除 </el-button> -->
    </span>
  </x-dialog>
</template>
<script>
  import { cloneDeep, omit } from '@package/utils'
  import { apiPostAddByOrganiz, apiPostEditByOrganiz } from '../api'
  import { EnumApiCode } from '../../../../../../globalEnum.js'
  // import { dictOptions } from '../config/buyerOrg.js'

  export default {
    data() {
      return {
        // dictOptions,
        title: false,
        visible: false,
        params: {
          purchaseGroupCode: '',
          purchaseGroupName: '',
          state: '1'
        },
        rules: {
          purchaseGroupName: [
            { required: true, message: '请输入采购组织名称', trigger: 'blur' },
            { min: 1, max: 30, message: '长度在 1 到 30 个字符 ', trigger: 'blur' }
          ],
          purchaseGroupCode: [
            { required: true, message: '请输入采购编码名称', trigger: 'blur' },
            { min: 1, max: 30, message: '长度在 1 到 30 个字符 ', trigger: 'blur' }
          ],
          state: [{ required: true, message: '请选择状态', trigger: 'blur' }]
        }
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        if (info?.id) {
          this.title = '编辑采购组织'
          this.$nextTick(() => {
            this.params = Object.assign(this.params, info)
          })
          return
        }
        this.params.parentId = info?.parentId ?? 0
        this.title = '新增采购组织'
      },
      handleSubmit() {
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params
            }
            const { code, msg } =
              'id' in this.params ? await apiPostEditByOrganiz(param) : await apiPostAddByOrganiz(param)
            if (code === EnumApiCode.Success) {
              this.$refs.params.resetFields()
              this.$message.success(
                'id' in this.params ? this.$t('system.user.editSuccess') : this.$t('system.user.newSuccess')
              )
              this.$emit('handleSuccess')
              this.visible = false
            } else {
              this.$message.error(msg)
            }
          } else {
            return false
          }
        })
      },
      handleClose() {
        this.$refs.params.resetFields()
        this.params = omit(this.params, 'id')
        this.visible = false
      }
    }
  }
</script>
